package com.jf.mall.dao.impl;

import com.jf.mall.dao.BasicDAO;
import com.jf.mall.dao.FurnitureDAO;
import com.jf.mall.domain.Furniture;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-26
 * Time:13:30
 */
public class FurnitureImpl extends BasicDAO<Furniture> implements FurnitureDAO {
    @Override
    public List<Furniture> queryFurniture() {
        String sql = "select id, `name`, maker, price, stock, sale, img_path imgPath from furniture";
        return queryMulti(sql, Furniture.class);
    }

    @Override
    public Furniture queryFurnitureById(int id) {
        String sql = "select id, `name`, maker, price, stock, sale, img_path imgPath from furniture where id = ?";
        return querySingle(sql, Furniture.class, id);
    }

    @Override
    public int addFurniture(Furniture furniture) {
        String sql = "insert into furniture(`name`, maker, price, stock, sale, img_path) values( ?, ?, ?, ?, ?, ?)";
        String name = furniture.getName();
        String maker = furniture.getMaker();
        BigDecimal price = furniture.getPrice();
        Integer stock = furniture.getStock();
        Integer sale = furniture.getSale();
        String imgPath = furniture.getImgPath();
        try {
            return update(sql, name, maker, price, stock, sale, imgPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteFurnitureById(int id) {
        String sql = "delete from furniture where id = ?";
        try {
            return update(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateFurniture(Furniture furniture) {
        String sql = "update furniture set `name` = ?, maker = ?, price = ?, stock = ?, sale = ?, img_path = ? where id = ?";
        String name = furniture.getName();
        String maker = furniture.getMaker();
        BigDecimal price = furniture.getPrice();
        Integer stock = furniture.getStock();
        Integer sale = furniture.getSale();
        String imgPath = furniture.getImgPath();
        Integer id = furniture.getId();
        return update(sql, name, maker, price, stock, sale, imgPath, id);
    }

    @Override
    public int getFurnitureCount() {
        String sql = "select count(*) from furniture";
        return ((Number) queryScalar(sql)).intValue();
    }

    @Override
    public List<Furniture> queryFurnitureByLimit(int start, int size) {
        String sql = "select id, `name`, maker, price, stock, sale, img_path imgPath from furniture limit ?,?";
        return queryMulti(sql, Furniture.class, start, size);
    }

    @Override
    public int getFurnitureCountByName(String name) {
        String sql = "select count(*) from furniture where `name` like ?";
        Object scalar = queryScalar(sql, "%" + name + "%");
        return ((Number) scalar).intValue();
    }

    @Override
    public List<Furniture> queryFurnitureByLimitAndName(int start, int size, String name) {
        String sql = "select id, `name`, maker, price, stock, sale, img_path imgPath from furniture where `name` like ? limit ?,?";
        return queryMulti(sql, Furniture.class, "%" + name + "%", start, size);
    }

    @Override
    public Map<Integer, Furniture> queryFurnitureMapByIds(Integer[] ids) {
        StringBuilder preSql = new StringBuilder("select id, `name`, maker, price, stock, sale, img_path imgPath from furniture where id in (?");
        for (int i = 0; i < ids.length - 1; i++) {
            preSql.append(",?");
        }
        preSql.append(")");
        String sql = preSql.toString();
        return queryMultiMap(sql, Furniture.class, "id", ids);
    }
}
